import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
// import Login from '../views/login/login.vue'
import Home from "../views/Home.vue";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    // name: 'Home',
    component: Home,
    children: [
      {
        path: "/home/index",
        name: "home",
        component: () => import("@/views/home/index.vue"),
        meta: {
          title: "数据可视化",
          icon: "HomeFilled",
          isShow: true,
        },
      },
      {
        path: "/table",
        name: "table",
        meta: {
          icon: "MessageBox",
          title: "超级表格",
          isShow: true,
        },
        children: [
          {
            path: "/table/useTable/index",
            name: "useTable",
            component: () => import("@/views/table/useTable/index.vue"),
            meta: {
              icon: "Grid",
              title: "用户列表",
              // isShow: true,
              isHide: false,
            },
          },
        ],
      },
      {
        path: "/form",
        name: "form",
        meta: {
          icon: "List",
          title: "超级表单",
          isShow: true,
        },
        children: [
          {
            path: "/form/useForm/index",
            name: "useForm",
            component: () => import("@/views/form/useForm/index.vue"),
            meta: {
              icon: "Grid",
              title: "基础表单",
              // isShow: true,
              isHide: false,
            },
          },
          {
            path: "/form/useForm2/index",
            name: "checkForm",
            component: () => import("@/views/form/useForm2/index.vue"),
            meta: {
              icon: "Grid",
              title: "校验表单",
              // isShow: true,
              isHide: false,
            },
          },
        ],
      },
      {
        path: "/echarts",
        name: "echarts",
        meta: {
          icon: "TrendCharts",
          title: "Echarts",
          isShow: true,
        },
        children: [
          {
            path: "/echarts/lineChart/index",
            name: "useEchart",
            component: () => import("@/views/echarts/lineChart/index.vue"),
            meta: {
              icon: "Grid",
              title: "折线图",
              // isShow: true,
              isHide: false,
            },
          },
          {
            path: "/echarts/histogram/index",
            name: "histogram",
            component: () => import("@/views/echarts/histogram/index.vue"),
            meta: {
              icon: "Grid",
              title: "柱状图",
              // isShow: true,
              isHide: false,
            },
          },
          {
            path: "/echarts/sector/index",
            name: "sector",
            component: () => import("@/views/echarts/sector/index.vue"),
            meta: {
              icon: "Grid",
              title: "扇形图",
              // isShow: true,
              isHide: false,
            },
          },
          {
            path: "/echarts/pressure/index",
            name: "pressure",
            component: () => import("@/views/echarts/pressure/index.vue"),
            meta: {
              icon: "Grid",
              title: "水型图",
              // isShow: true,
              isHide: false,
            },
          },
          {
            path: "/echarts/radar/index",
            name: "radar",
            component: () => import("@/views/echarts/radar/index.vue"),
            meta: {
              icon: "Grid",
              title: "雷达图",
              // isShow: true,
              isHide: false,
            },
          }
        ],
      },
      {
        path: "/embedded",
        name: "embedded",
        meta: {
          icon: "Ticket",
          title: "内嵌",
          isShow: true,
        },
        children: [
          {
            path: "/embedded/index",
            name: "embed",
            component: () => import("@/views/embedded/index.vue"),
            meta: {
              icon: "Grid",
              title: "内嵌页面",
              // isShow: true,
              isHide: false,
            },
          },
        ],
      },
      {
        path: "/instruct",
        name: "instruct",
        meta: {
          icon: "Stamp",
          title: "自定义指令",
          isShow: true,
        },
        children: [
          {
            path: "/instruct/copy/index",
            name: "copy",
            component: () => import("@/views/instruct/copy/index.vue"),
            meta: {
              icon: "Grid",
              title: "复制指令",
              // isShow: true,
              isHide: false,
            },
          },
          {
            path: "/instruct/throttle/index",
            name: "throttle",
            component: () => import("@/views/instruct/throttle/index.vue"),
            meta: {
              icon: "Grid",
              title: "节流指令",
              // isShow: true,
              isHide: false,
            },
          },
          {
            path: "/instruct/bracket/index",
            name: "bracket",
            component: () => import("@/views/instruct/bracket/index.vue"),
            meta: {
              icon: "Grid",
              title: "防抖指令",
              // isShow: true,
              isHide: false,
            },
          },
        ],
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    meta: {
      isShow: false,
    },
    component: () => import("@/views/login/login.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem("token");
  if (token) {
    if (to.name === "Home") {
      next({
        path: "/home/index",
      });
    } else {
      next();
    }
  } else {
    if (to.path !== "/login" && to.name !== "404") {
      next({
        path: "/login",
      });
    } else {
      next();
    }
    if (to.path !== "/404" && to.name === "404") {
      next({
        path: "/404",
      });
    } else {
      next();
    }
    return false;
  }
});

export default router;
